﻿package view.npc{
	/***********************************
	NPC在场景中的位置：第一个NPC x = 300；第一个NPC x = 1024
	活动范围：出现位置的前后上下50单位
	***********************************/
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import view.player.*;
	
	public class MyNPC extends MovieClip
	{
		protected var m_InitX:Number;                  //NPC初始位置
		protected var m_InitY:Number;
		protected var ThinkingTime:int = 6;            //NPC思考时间
		public static var STATE_STOP:int = 0;
		public static var STATE_MOVELEFT:int = 1;      //NPC移动
		public static var STATE_MOVERIGHT:int = 2;
		public static var STATE_MOVEUP:int = 3;
		public static var STATE_MOVEDOWN:int = 4;
		public static var STATE_TALK:int = 7;          //NPC 与玩家的对话状态
		protected var m_nState:int = STATE_MOVERIGHT;       //初始状态
		protected var m_Speed:Number;                  //速度
		protected var m_LastX:Number;                  //记录X
		protected var m_LastY:Number;                  //记录Y
		private var m_akeys:Array;                               //记录键盘

		public function MyNPC() 
		{
			this.stop();
		}
		public function setState(sta:int):void              //设置状态
		{
			m_nState = sta;
			//trace(m_nState);
		}
		
		public function setInitPos(px:Number,py:Number):void     //设置NPC位置
		{
			this.x = px;
			this.y = py;
			m_LastX = this.x;
			m_LastY = this.y;
			m_InitX = this.x;
			m_InitY = this.y;
		}
		
		public function Input(object:Player,keys:Array):void           //键盘输入
		{
		 	for each (var key:int in keys){
				if(this.x > object.x - 100 && this.x < object.x + 100 &&key == Keyboard.SPACE)                 //如果按下空格键，则执行Talking
					{Talking(7,10);}
			}
			
			/*else
			    return;*/
		}
		
		public function NPCLogic(object:Player):void             //NPC逻辑
		{
				Logic();
				Thinking(object);
		}
		
		public function Logic():void               //处理逻辑，控制移动
		{
			switch(m_nState)
			{
				
				case STATE_MOVELEFT:
				      Move(1,3,-15,0);                //1~3帧是NPC向左走，没有对话框
					  break;
				case STATE_MOVERIGHT:
				      Move(4,6,15,0);                 //4~6是NPC向右走，没有对话框
					  break;
			}
		}
		
		public function Thinking(object:Player):void            //NPC思考
		{
			if(ThinkingTime > 0)
			{
				ThinkingTime--;
				return;
			}
			ThinkingTime = 6;
			if (this.x - object.x > 100 || this.x - object.x < -100 || this.y - object.y < -100) {
				var sta:int = Math.random() * 3;           //在0、1、2三种状态中随机选择一种
			setState(sta);
			}
			
		}
		
		public function Talking(startFrame:int,endFrame:int):void             //NPC与玩家对话状态
		{	setState(STATE_TALK)
			if(this.currentFrame < startFrame /*|| this.currentFrame > endFrame*/) 
			{
				/*trace(this.currentFrame);*/
				gotoAndStop(startFrame);
			}
			else if(this.currentFrame<endFrame)                  
			{
					nextFrame();
			}
			else if(this.currentFrame==endFrame)
			{
				var sta:int = Math.random() * 3;           //在0、1、2三种状态中随机选择一种
			    setState(sta);
			}
		}
		
		protected function Move(startFrame:int,endFrame:int,SpeedX:Number,SpeedY:Number):void //控制NPC移动的函数
		{
			if(this.currentFrame < startFrame || this.currentFrame >= endFrame)
			{
				gotoAndStop(startFrame);
			}
			else
			{			    
				nextFrame();
			}
			m_LastX = this.x;
			m_LastY = this.y;
			this.x = this.x + SpeedX;
			this.y = this.y + SpeedY;
			if(this.x > m_InitX + 32 || this.x < m_InitX - 32 ||this.y > m_InitY + 64 || this.y < m_InitY - 64)//控制NPC移动的范围
				{
					MoveBack();
				}
		}
		
		
		public function MoveBack():void                  //NPC超过一定移动范围后往回走
		{
			this.x = m_LastX;
			this.y = m_LastY;
			}
		
		
		/*
		public function setDialog(bDialog:Boolean):void
		{
			if(bDialog==false)
			{
				return;
			}
			else
			{
				setState(STATE_TALK);//状态变为talk模式
			}
		}*/
		
	}
	
}
